import Taro from '@tarojs/taro';

import {Command} from '../constant';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';

import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },

    async status() {
      // 或取当前状态值
      let {
        main: {isLoading},
      } = getData();
      action.commonChange('main.isLoading', !isLoading);
    },

    /**
     * 查询ip列表
     */
    async loadIpItems(payload) {
      console.log('action.ts sythetciCardLibrary loadCardItems payload', payload);

      action.commonChange('main.ipItems', []);
      action.commonChange('main.response.loadIp', '');
      action.commonChange('main.status.loadingIp', true);

      let { data, code, message } = await api.SyntheticCardController.getIpItems(payload);

      console.log('action.ts loadIpItems data', data)

      action.commonChange('main.ipItems',  data || []);
      action.commonChange('main.response.loadIp', code);
      action.commonChange('main.status.loadingIp', false);
      
    },

    /**
     * 查询卡库列表
     */
    async loadCardItems(payload) {
      console.log('action.ts loadCardItems payload', payload)

      action.commonChange('main.cardItems', []);
      action.commonChange('main.response.loadCard', '');
      action.commonChange('main.status.loadingCard', true);

      let { data, code, message } = await api.SyntheticCardController.getCardItems(payload);
      console.log('actin.ts loadCardItems data', data)


      action.commonChange('main.cardItems', data || []);
      action.commonChange('main.response.loadIp', code);
      action.commonChange('main.status.loadingCard', false);
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageSyntheticCardLibraryPaMain'),
  };
}
